package com.markyao.utils;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;

public class CacheUtils {

    public static Cache<String,String> getStrStrCache(int cap){
        Cache<String, String> cache = Caffeine.newBuilder()
                .expireAfter(new Expiry<String, String>() {
                    @Override
                    public long expireAfterCreate(@NonNull String s, @NonNull String s2, long l) {
                        return l;
                    }

                    @Override
                    public long expireAfterUpdate(@NonNull String s, @NonNull String s2, long l, @NonNegative long l1) {
                        return l1;
                    }

                    @Override
                    public long expireAfterRead(@NonNull String s, @NonNull String s2, long l, @NonNegative long l1) {
                        return l1;
                    }
                })
                .initialCapacity(cap)
                .build();
        return cache;
    }

    public static Cache<String,Integer> getStrIntCache(int cap){
        Cache<String, Integer> cache = Caffeine.newBuilder()
                .expireAfter(new Expiry<String, Integer>() {
                    @Override
                    public long expireAfterCreate(@NonNull String s, @NonNull Integer s2, long l) {
                        return l;
                    }

                    @Override
                    public long expireAfterUpdate(@NonNull String s, @NonNull Integer s2, long l, @NonNegative long l1) {
                        return l1;
                    }

                    @Override
                    public long expireAfterRead(@NonNull String s, @NonNull Integer s2, long l, @NonNegative long l1) {
                        return l1;
                    }
                })
                .initialCapacity(cap)
                .build();
        return cache;
    }
}
